package com.cwd.arm.application.service.read;

import com.cwd.arm.common.util.SessionUtil;
import com.cwd.arm.infrastructure.dao.MsgDao;
import com.cwd.arm.infrastructure.dao.MsgRecipientDao;
import com.cwd.arm.infrastructure.po.MsgPo;
import com.cwd.arm.application.dto.MsgDto;
import com.cwd.arm.application.service.read.facade.MsgReadService;
import com.cwd.arm.infrastructure.query.MsgDaoQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
* Description:消息
* Created on 2022年01月15日
* @author  yan
*/
@Repository
@Primary
public class MsgReadServiceImpl implements MsgReadService {

    @Autowired
    private MsgDao msgDao;

    @Autowired
    private MsgRecipientDao msgRecipientDao;

    @Override
    public MsgDto selectById(Long id) {
        MsgPo po = msgDao.getById(id);
        msgRecipientDao.readByCurrentUser(id);
        return convertToDto(po);
    }

    @Override
    public List<MsgDto> getCurrentUserMsgList() {
        return convertToDtoList(msgDao.selectByCurrentUser());
    }

    private List<MsgDto> convertToDtoList(List<MsgPo> poList){
        return poList.stream().map(po->convertToDto(po)).collect(Collectors.toList());
    }

    private MsgDto convertToDto(MsgPo po){
        MsgDto dto = new MsgDto();
        BeanUtils.copyProperties(po, dto);
        return dto;
    }

}
